package com.ltxtiyu.video.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ltxtiyu.video.common.JsonTool;
import com.ltxtiyu.video.entity.User;
import com.ltxtiyu.video.service.app.OauthService;
import com.ltxtiyu.video.service.app.UserService;
import com.ltxtiyu.video.tool.ParamTool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/apiOauth")
public class ApiOauth {
    @Reference
    UserService userService;
    @Reference
    OauthService oauthService;

    /**
     * 发送验证码
      * @param request
     * @return
     */
    @RequestMapping("/captcha")
    public String captcha(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.captcha(stringObjectMap);
    }

    /**
     * 手机号注册
     * @param request
     * @return
     */
    @RequestMapping("/register")
    public String register(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.register(stringObjectMap);
    }

    /**
     * 密码登陆
     * @param request
     * @return
     */
    @RequestMapping("/loginByPassword")
    public String loginByPassword(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.loginByPassword(stringObjectMap);
    }

    /**
     * 密码登陆
     * @param request
     * @return
     */
    @RequestMapping("/loginByWx")
    public String loginByWx(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.loginByWx(stringObjectMap);
    }

    /**
     * 忘记密码 | 找回密码
     *
     * @param request
     * @return
     */
    @RequestMapping("/setPassword")
    public String forgotPassword(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.setPassword(stringObjectMap);
    }

    /**
     * 刷新token
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/refreshToken")
    public String refreshToken(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.refreshToken(stringObjectMap);
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        Map<String, String> stringObjectMap = ParamTool.requestToParamMap(request);
        return oauthService.logout(stringObjectMap);
    }

}
